package com.tgjorgoski.sidekicknotes.rtfview.support;

import java.awt.event.ActionEvent;

import javax.swing.Action;
import javax.swing.JEditorPane;
import javax.swing.text.*;

public class SubSuperStrikeSupport {
	public static final String SUPERSCRIPT_KEY="font-superscript";
	public static final String SUBSCRIPT_KEY="font-subscript";
	public static final String STRIKETHROUGH_KEY="font-strikethrough";

	private static SubSuperStrikeSupport instance;
	
	private Action subscriptAction, superscriptAction, strikeThroughAction;
	
	public static SubSuperStrikeSupport getInstance() {
		if (instance==null) {
			instance=new SubSuperStrikeSupport();
		}
		return instance;
	}
	
	private SubSuperStrikeSupport() {
		subscriptAction = new SubscriptAction();
		superscriptAction = new SuperscriptAction();
		strikeThroughAction = new StrikeThroughAction();
	}
	
	public void registerComponent(JTextComponent comp) {
		comp.getActionMap().put(SUPERSCRIPT_KEY, superscriptAction);
		comp.getActionMap().put(SUBSCRIPT_KEY, subscriptAction);
		comp.getActionMap().put(STRIKETHROUGH_KEY, strikeThroughAction);
	}

	class SubscriptAction extends StyledEditorKit.StyledTextAction {
		public SubscriptAction() {
			super(StyleConstants.Subscript.toString());
		}

		public void actionPerformed(ActionEvent ae) {
			JEditorPane editor = getEditor(ae);
			if (editor != null) {
				StyledEditorKit kit = getStyledEditorKit(editor);
				MutableAttributeSet attr = kit.getInputAttributes();
				boolean subscript = (StyleConstants.isSubscript(attr)) ? false
						: true;
				SimpleAttributeSet sas = new SimpleAttributeSet();
				StyleConstants.setSubscript(sas, subscript);
				setCharacterAttributes(editor, sas, false);
			}
		}
	}

	class SuperscriptAction extends StyledEditorKit.StyledTextAction {

		public SuperscriptAction() {
			super(StyleConstants.Superscript.toString());
		}

		public void actionPerformed(ActionEvent ae) {
			JEditorPane editor = getEditor(ae);
			if (editor != null) {
				StyledEditorKit kit = getStyledEditorKit(editor);
				MutableAttributeSet attr = kit.getInputAttributes();
				boolean superscript = (StyleConstants.isSuperscript(attr)) ? false
						: true;
				SimpleAttributeSet sas = new SimpleAttributeSet();
				StyleConstants.setSuperscript(sas, superscript);
				setCharacterAttributes(editor, sas, false);
			}
		}
	}

	class StrikeThroughAction extends StyledEditorKit.StyledTextAction {

		public StrikeThroughAction() {
			super(StyleConstants.StrikeThrough.toString());
		}

		public void actionPerformed(ActionEvent ae) {
			JEditorPane editor = getEditor(ae);
			if (editor != null) {
				StyledEditorKit kit = getStyledEditorKit(editor);
				MutableAttributeSet attr = kit.getInputAttributes();
				boolean strikeThrough = (StyleConstants.isStrikeThrough(attr)) ? false
						: true;
				SimpleAttributeSet sas = new SimpleAttributeSet();
				StyleConstants.setStrikeThrough(sas, strikeThrough);
				setCharacterAttributes(editor, sas, false);
			}
		}
	}
}
